
import ls from "../local-storage";
import { getUserInfo } from "./user";
import WebResult, { StatusCode } from "./web-result";

export async function getDiscussion() {
    const disscusionData = ls.get("discussion");
    if (disscusionData) {
        return WebResult.success(disscusionData);
    }
    return WebResult.success([]);
}

export async function publishDiscussion(content) {
    const userInfoResult = await getUserInfo();
    if (userInfoResult.code === StatusCode.FAILED) {
        return WebResult.failed(undefined, "未登录");
    }
    const user = userInfoResult.data;
    if (!user) {
        return WebResult.failed(undefined, "未知错误");
    }
    const username = user.username;
    const nowDate = new Date();
    const time = nowDate.toLocaleDateString() + " " + nowDate.toLocaleTimeString();
    const data = (await getDiscussion()).data
    data.push({
        username,
        time,
        content,
    });
    ls.set("discussion", data);
    return WebResult.success(undefined, "发布成功");
}


export async function clearDiscussion() {
    ls.remove("discussion");
    return WebResult.success(undefined, "清除成功");
}

